﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Configuration;
/// <summary>
///EmailHelper 的摘要说明
/// </summary>
public class EmailHelper
{
    public static string SmtpServer = "smtp.163.com";
    public static string FormUserName = "";
    public static string FormUserPasswd = "";

    static EmailHelper()
    {
        try
        {
            SmtpServer = ConfigurationManager.AppSettings["SmtpServer"];
            FormUserName = ConfigurationManager.AppSettings["FormUserName"];
            FormUserPasswd = ConfigurationManager.AppSettings["FormUserPasswd"];
        }
        catch
        { }
    }

    /// <summary>  
    /// 发送邮件  
    /// </summary>  
    /// <param name="tomail">收件人邮件地址</param>  
    /// <param name="title">标题</param>  
    /// <param name="content">邮件正文</param>  
    /// <param name="FormUser">发件人账号</param>  
    /// <param name="userPwd">发件人密码</param>  
    public static void sendEmail(string tomail, string title, string content, string formUser, string userPwd)
    {
        sendEmail(tomail, title, content, formUser, SmtpServer);
    }

    /// <summary>  
    /// 发送邮件  
    /// </summary>  
    /// <param name="tomail">收件人邮件地址</param>  
    /// <param name="title">标题</param>  
    /// <param name="content">邮件正文</param>  
    /// <param name="FormUser">发件人账号</param>  
    /// <param name="userPwd">发件人密码</param>  
    /// <param name="smtpServer"></param>
    public static void sendEmail(string tomail, string title, string content, string FormUser, string userPwd, string smtpServer)
    {
        MailAddress from = new MailAddress(FormUser);
        MailAddress to = new MailAddress(tomail);
        MailMessage MyMessage = new MailMessage(from, to);
        MyMessage.Priority = MailPriority.Normal;
        MyMessage.Priority = MailPriority.Normal;

        MyMessage.IsBodyHtml = false;
        MyMessage.IsBodyHtml = true;
        MyMessage.Body = content;
        MyMessage.BodyEncoding = System.Text.Encoding.UTF8;
        MyMessage.Subject = title;
        //string SmtpServer = "smtp.163.com";//这里使用你服务器端发送的邮箱smtp协议  # #新浪邮箱的：<strong></strong><pre name="code" class="csharp" style="display: inline !important; "><pre name="code" class="csharp" style="display: inline !important; ">smtp.sina.com网易邮箱："smtp.126.com";  QQ邮箱：smtp.qq.com  
        //SmtpClient client = new SmtpClient(SmtpServer);
        SmtpClient client = new SmtpClient(smtpServer);
        System.Net.NetworkCredential cred = new System.Net.NetworkCredential(FormUser, userPwd);
        client.Credentials = cred;
        client.Send(MyMessage);
    }
}